﻿
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.IO;
using LeeAnn;
namespace Demo1
{
    // PrimitiveBatch is a class that handles efficient rendering automatically for its
    // users, in a similar way to SpriteBatch. PrimitiveBatch can render lines, points,
    // and triangles to the screen. In this sample, it is used to draw a spacewars
    // retro scene.
    public class DrawGeometry
    {
        public Game game;

        GraphicsDevice gdevice;

        public Texture2D pixel;
        public SpriteBatch spritebatch;

        Rectangle sourcerect = new Rectangle(0, 0, 1, 1);

        public DrawGeometry(Game game)
        {
            this.game = game;
            IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService));
            gdevice = graphicsService.GraphicsDevice;
            spritebatch = new SpriteBatch(gdevice);
        }

        // create a 1x1 pixel texture with white color for optimal tint 
        private void createPixelTexture()
        {
            pixel = new Texture2D(gdevice, 1, 1, false,SurfaceFormat.Color);
            pixel.SetData<Color>(new Color[1]{Color.Red});
            //Surface surf = pixel.GetSurfaceLevel(0);
           // Surface oldRenderTarget = gdevice.GetRenderTarget(0);
            //gdevice.SetRenderTarget(0, surf);
            //gdevice.Clear(Color.White);
            //gdevice.SetRenderTarget(0, oldRenderTarget);
        }

        public void drawPoint(Vector2 point, Color color)
        {
            spritebatch.Begin();
            spritebatch.Draw(pixel, point, color);
            spritebatch.End();
        }

        public void drawLine(Vector2D p1, Vector2D p2, Color color)
        {
            if (pixel == null)
                createPixelTexture();

            float angle = (float)Math.Atan2(p2.Y - p1.Y, p2.X - p1.X);
            Vector2D diff = p2 - p1;

            spritebatch.Begin();
            spritebatch.Draw(pixel, new Vector2(p1.X, p1.Y), null, color,angle, Vector2.Zero, new Vector2((float)diff.Length(), 1),SpriteEffects.None, 0);
            spritebatch.End();
        }

        public void drawRectangle(Rectangle rectangle, Color color, bool center)
        {
            Vector2D TopLeft, TopRight, BottomLeft, BottomRight;
            Vector2D offset = new Vector2D(0,0);
            if (center)
                offset = (new Vector2D(rectangle.Center.X,rectangle.Center.Y) - new Vector2D(rectangle.Left, rectangle.Top));
            TopLeft = new Vector2D(rectangle.Left - offset.X, rectangle.Top - offset.Y);
            TopRight = new Vector2D(rectangle.Right - offset.X, rectangle.Top - offset.Y);
            BottomLeft = new Vector2D(rectangle.Left - offset.X, rectangle.Bottom - offset.Y);
            BottomRight = new Vector2D(rectangle.Right - offset.X, rectangle.Bottom - offset.Y);
            drawLine(TopLeft, TopRight, color);
            drawLine(TopLeft, BottomLeft, color);
            drawLine(TopRight, BottomRight, color);
            drawLine(BottomLeft, BottomRight, color);
        }

        public void filledRectangle(Rectangle rectangle, Color color)
        {
            if (pixel == null)
                createPixelTexture();

            spritebatch.Begin();
            spritebatch.Draw(pixel, rectangle, color);
            spritebatch.End();
        }
    }

}
